import requests
import json
import os
base_url=os.getenv("BASE_URL")
def fetch_albums_by_singer(singer_mid: str, page_no=1, page_size=20):
    url = f"{base_url}/singer/album?singermid={singer_mid}&pageNo={page_no}&pageSize={page_size}"
    response = requests.get(url)
    
    if response.status_code != 200:
        raise Exception(f"请求失败，状态码：{response.status_code}")

    data = response.json()
    album_list = data.get("data", {}).get("list", [])
    
    result = []
    for album in album_list:
        song_count = album.get("latest_song", {}).get("song_count", 0)
        if song_count > 1:
            # 获取歌手信息数组
            singers = [
                {
                    "singer_id": s.get("singer_id"),
                    "singer_mid": s.get("singer_mid"),
                    "singer_name": s.get("singer_name")
                }
                for s in album.get("singers", [])
            ]
            result.append({
                "album_mid": album.get("album_mid"),
                "album_name": album.get("album_name"),
                "song_count": song_count,
                "singers": singers
            })
    
    return result

# 调用函数
if __name__ == "__main__":
    singer_mid = "0025NhlN2yWrP4"  # 周杰伦
    albums = fetch_albums_by_singer(singer_mid)
    print(json.dumps(albums, indent=2, ensure_ascii=False))